package com.wn.demo01;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author yellow Docter
 * @date 2022 -10-28
 * @desc
 *  java系统中自带的函数接口
 *  4 大函数式接口
 *  1、 消费性接口          Consumer             数据进行消费
 *  2、 供给型接接口        T Supplier           返回数据
 *  3、 函数型接口         Function             T 返回  R
 *  4、 断定型接           Predicate            条件判断
 */
public class Demo03 {

    public static void main(String[] args) {

//        demo01();

        //供给型接接口
//        List<Integer> list = demo02(()->new Random().nextInt(10));
//        System.out.println("list = " + list);


        //函数式接口

//        String str = demo03("钢铁",s->"电动汽车");
//        System.out.println("str = " + str);
//
//        User user = demo04("张三",name->new User(name));
//        System.out.println("user = " + user);

        List<Integer> data = Arrays.asList(10, 12, 24);
        List<Integer> list = demo05(data,t->t>15);
        System.out.println("list = " + list);

    }

    private static List<Integer> demo05(List<Integer> list, Predicate<Integer> predicate) {

        List<Integer> newlist =new ArrayList<>();

        for (Integer integer : list) {

            if (predicate.test(integer)){   //自己的条件过滤 (断定接口自己实现)
                newlist.add(integer);
            }

        }
        return newlist;
    }

    private static User demo04(String name,Function<String,User> function) {
        return function.apply(name);
    }

    private static String demo03(String msg,Function<String,String> function) {
        return function.apply(msg);
    }

    private static List<Integer> demo02(Supplier<Integer> supplier) {

        List<Integer> list =new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(supplier.get());
        }
        return list;

    }


    //消费性接口
    private static void demo01() {

        //这个接口的具体的运用
        // stream的终止操作
        Consumer<String> consumer = s-> System.out.println(s);
        consumer.accept("吃饭花了20￥");

        //流的终止操作 
        Arrays.asList("aa","bb").stream().forEach(s -> System.out.println(s));

    }

}
